package com.yinhai.activity;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.yinhai.contacts.adapter.ContactsSameListAdapter;
import com.yinhai.contacts.bean.SameContactAda;
import com.yinhai.contacts.util.ContactsUtils;
import com.yinhai.model.CloudNote;
import com.yinhai.util.GetCurrentTime;


public class ContactsSameListActivity extends Activity {
	ListView contactsSameListView;
	TextView description;
	ImageView btn_back;
	List<SameContactAda> list = new ArrayList<SameContactAda>();
	ContactsSameListAdapter cslAdapter;
	View note_main,navRecWindow;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contactsamelist);
		registerReceiver(clearSameContactReceiver,new IntentFilter("com.clearsamecontact.update"));
		btn_back = (ImageView)findViewById(R.id.btn_back);
		note_main = (View)findViewById(R.id.note_main);
		description = (TextView)findViewById(R.id.description);
		contactsSameListView = (ListView) findViewById(R.id.listview);
		cslAdapter = new ContactsSameListAdapter(ContactsSameListActivity.this,list);
		btn_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		description.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				LayoutInflater factory = LayoutInflater.from(ContactsSameListActivity.this);
				View descView = factory.inflate(
						R.layout.pop_clearsame_description, null);
				AlertDialog dlg = new AlertDialog.Builder(
						ContactsSameListActivity.this)
						.setIcon(0)
						.setTitle("功能说明与服务条款")
						.setView(descView)
						.setNegativeButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										dialog.dismiss();
									}
								}).create();
				dlg.show();
			}
		});
		init();

	}
	void init() {
		new ContactSameListAsyncTask().execute();
	}

	private class ContactSameListAsyncTask extends
			AsyncTask<Void, Void, Integer> {
		@Override
		protected void onPostExecute(Integer result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			//popSortListView.setAdapter(adapter);
			contactsSameListView.setAdapter(cslAdapter);
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected Integer doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if(list!=null){
				list.clear();
			}
			LinkedList<String> nameList = ContactsUtils._getContactsPhoneOrName(ContactsSameListActivity.this.getContentResolver(), 0);
			LinkedList<String> phoneList = ContactsUtils._getContactsPhoneOrName(ContactsSameListActivity.this.getContentResolver(), 1);
			List<String> nameList1 = ContactsUtils.CheckOverlap(nameList);
			List<String> phoneList1 = ContactsUtils.CheckOverlap(phoneList);
			List<SameContactAda> list1 = new ArrayList<SameContactAda>();
			List<SameContactAda> list2 = new ArrayList<SameContactAda>();
			if(nameList1.size()>0){
				list1 = ContactsUtils.getAdapterList(ContactsSameListActivity.this.getContentResolver(), nameList1, 0);
			}
			if(phoneList1.size()>0){
				list2 = ContactsUtils.getAdapterList(ContactsSameListActivity.this.getContentResolver(), phoneList1, 1);
			}
			if(list1.size()>0){
				list.addAll(list1);
			}
			if(list2.size()>0){
				list.addAll(list2);
			}
			return 0;
		}

	}
	
	private BroadcastReceiver clearSameContactReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getAction().equals("com.clearsamecontact.update")) {
				new ContactSameListAsyncTask().execute();
			}
		}
		
	};
	
	@Override
	public void onDestroy() {
		unregisterReceiver(clearSameContactReceiver);
		super.onDestroy();
	}

}
